package activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.laoxinwen.app.R;
import com.umeng.analytics.MobclickAgent;

import java.util.Random;

import bean.Topic;
import bean.wraper.DefaultStringWrapper;
import common.HttpCallBack;
import common.Requester;
import common.ServerConfig;
import common.UserConfig;
import common.VideoWebChromeClient;
import share.LoginHelper;
import share.LoginType;
import share.ShareActivity;
import share.ShareDialog;

public class H5TopicActivity extends ShareActivity implements View.OnClickListener, LoginHelper.LoginResultListener, VideoWebChromeClient.VideoFullScreenHandler {

    public static final String INTENT_KEY_NEWSGROUP = "newsgroup";

    private LoginHelper loginHelper;
    private Dialog shareDialog;
    private Topic topic;

    @id(R.id.topbar)
    private FrameLayout topbar;

    @id(R.id.webView)
    private WebView webView;

    @id(R.id.tv_topic_title)
    private TextView topicTitle;

    @id(R.id.fullWebViewContainer)
    private FrameLayout fullWebViewContainer;

    private VideoWebChromeClient webChromeClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5_topic);
        loadView();

        loginHelper = new LoginHelper(this);
        loginHelper.setLoginResultListener(this);

        topic = (Topic) getIntent().getSerializableExtra(INTENT_KEY_NEWSGROUP);
        topicTitle.setText(topic.getTitle());

        webChromeClient = new VideoWebChromeClient(webView, fullWebViewContainer, this);
        webView.setWebChromeClient(webChromeClient);
        WebSettings webSettings = webView.getSettings();
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webSettings.setUseWideViewPort(true);
//        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setJavaScriptEnabled(true);

        String url = ServerConfig.SERVER_HOST + "/topic/" + topic.getId() + "?app=" + ServerConfig.API_VERSION + "&h5";
        webView.loadUrl(url);
    }

    @Override
    public void onFullScreen() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        topbar.setVisibility(View.GONE);
    }

    @Override
    public void onExitFullScreen() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        topbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onBackPressed() {
        if (webChromeClient.inCustomView()) {
            webChromeClient.onHideCustomView();
        } else if (webView.canGoBack()) {
            webView.goBack();
        }
        else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
    }

    public static Intent getIntent(Context context, Topic topic) {
        Intent it = new Intent(context, H5TopicActivity.class);
        it.putExtra(INTENT_KEY_NEWSGROUP, topic);
        return it;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_share:
                shareTopic();
                break;
        }
    }

    private void shareTopic() {
        String url = ServerConfig.SERVER_HOST + "/topic/" + topic.getId();
        String userId = UserConfig.isLogined() ? UserConfig.getUserInfo().getId() : "0";
        Random r = new Random(System.currentTimeMillis());
        int random = r.nextInt(Integer.MAX_VALUE) + 1000;
        url += "?stamp=" + userId + "_" + System.currentTimeMillis() + random;
        final String srcUrl = url;
        shareDialog = ShareDialog.getShareDialog(this, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareDialog.dismiss();
                switch (v.getId()) {
                    case R.id.share_qq:
                        shareToQq(topic.getTitle(), topic.getSummary(), ServerConfig.PIC_HOST + topic.getImg(), srcUrl);
                        break;
                    case R.id.share_qq_zone:
                        shareToQqZone(topic.getTitle(), topic.getSummary(), ServerConfig.PIC_HOST + topic.getImg(), srcUrl);
                        break;
                    case R.id.share_weibo:
                        shareToWeibo(topic.getTitle(), topic.getSummary(), ServerConfig.PIC_HOST + topic.getImg(), srcUrl);
                        break;
                    case R.id.share_weixin:
                        shareToWeixin(topic.getTitle(), topic.getSummary(), ServerConfig.PIC_HOST + topic.getImg(), srcUrl);
                        break;
                    case R.id.share_weixin_friends:
                        shareToWeixinFriendCircle(topic.getTitle(), topic.getSummary(), ServerConfig.PIC_HOST + topic.getImg(), srcUrl);
                        break;
                    case R.id.share_url:
                        shareCopyUrl(srcUrl);
                        break;
                }
                Requester.shareLog("topic", topic.getId(), new HttpCallBack<DefaultStringWrapper>() {
                    @Override
                    public void onSucceed(DefaultStringWrapper data) {

                    }
                });
            }
        });
        shareDialog.show();
    }

    private void showLoginDialog() {
        loginHelper.showDialog();
    }

    @Override
    public void onLoginFail() {
        loginHelper.dismissDialog();
    }

    @Override
    public void onLoginSucceed() {
        loginHelper.dismissDialog();
    }

    @Override
    protected void onQqInfoObtained() {
        loginHelper.login(LoginType.QQ);
    }

    @Override
    protected void onWeiboInfoObtained() {
        loginHelper.login(LoginType.SINA);
    }

    @Override
    protected void onWechatInfoObtain() {
        loginHelper.login(LoginType.WECHAT);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("H5专题页");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("H5专题页");
    }
}
